﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TranTekBE.Trac_Nghiem
{
    public partial class ThongKeCauHoi : PageRequireLogin
    {
        public Database database = new Database(TinyLib.GetConfigKey("ConnectionString2"));
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                var ds = database.ExecuteQuery("SELECT * FROM tbl_DailyCategory");
                var dt = ds.Tables[0];
                ddlCategory.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlCategory.Items.Add(new ListItem() { Value = dt.Rows[i]["CategoryId"].ToString(), Text = dt.Rows[i]["Name"].ToString() });
                }
                ViewState["SortExpression"] = "Total ASC";
                BindGridView();
            }

            string sql = "SELECT COUNT(*) FROM tbl_DailyQuestion ";
            if (!string.IsNullOrEmpty(ddlCategory.SelectedValue))
            {
                sql += " WHERE CategoryId=" + ddlCategory.SelectedValue;
            }
            LiteralTongSo.Text = ThuVien.FunctionsUtils.ConvertToInt(database.ExecuteScalar(sql).ToString(), 0).ToString();
        }
        DataTable LoadData()
        {
            DataTable dt = new DataTable();


            string sql = @"SELECT l.* FROM vThongKeCauHoi l LEFT JOIN tbl_DailyQuestion q
	                        ON l.QuestionId=q.QuestionId
	                        WHERE l.[Content] LIKE N'%{0}%' AND q.CategoryId={1}";
            string sql1 = @"SELECT c.* FROM vThongKeChiTietDapAn c LEFT JOIN tbl_DailyQuestion q ON c.QuestionId=q.QuestionId
                            WHERE q.Content LIKE N'%{0}%' AND q.CategoryId={1}";

            var ds = database.ExecuteQuery(string.Format(sql1, txtSearch.Text, ddlCategory.SelectedValue));
            DataTable dtDa = ds.Tables[0];

            ds = database.ExecuteQuery(string.Format(sql, txtSearch.Text, ddlCategory.SelectedValue));
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            //tạo table return
            DataTable table = new DataTable();
            for (int x = 0; x < dt.Columns.Count; x++)
            {
                table.Columns.Add(dt.Columns[x].ColumnName);
            }
            table.Columns.Add("DA1");
            table.Columns.Add("DA2");
            table.Columns.Add("DA3");
            table.Columns.Add("DA4");
            table.Columns.Add("DA5");
            table.Columns.Add("DA6");
            table.Columns["DA1"].DataType = typeof(int);
            table.Columns["DA2"].DataType = typeof(int);
            table.Columns["DA3"].DataType = typeof(int);
            table.Columns["DA4"].DataType = typeof(int);
            table.Columns["DA5"].DataType = typeof(int);
            table.Columns["DA6"].DataType = typeof(int);
            table.Columns["Total"].DataType = typeof(int);
            table.Columns["Dung"].DataType = typeof(int);
            table.Columns["Sai"].DataType = typeof(int);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = table.NewRow();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    row[j] = dt.Rows[i][j];
                    int a = 1;

                    for (int n = 0; n < dtDa.Rows.Count; n++)
                    {
                        string qid = dt.Rows[i]["QuestionId"].ToString();
                        if (dtDa.Rows[n]["QuestionId"].ToString() == qid)
                        {
                            int per = ThuVien.FunctionsUtils.ConvertToInt(string.Format("{0:0}", float.Parse(dtDa.Rows[n]["Per"].ToString())), 0);
                            row["DA" + a] = per;
                            a++;

                        }
                    }
                }
                table.Rows.Add(row);
            }

            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    int a = 1;
            //    for (int j = 0; j < dtDa.Rows.Count; j++)
            //    {
            //        if (dtDa.Rows[j]["QuestionId"].ToString() == dt.Rows[i]["QuestionId"].ToString())
            //        {
            //            dt.Rows[i]["DA" + a] = ThuVien.FunctionsUtils.ConvertToInt(string.Format("{0:0}", float.Parse(dtDa.Rows[j]["Per"].ToString())), 0);
            //            a++;
            //        }
            //    }
            //}

            return table;

        }
        void ClearGridview()
        {
            while (GridView1.Rows.Count > 0)
            {
                GridView1.DeleteRow(0);
            }
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridView();
            //LoadData();
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            //   LoadData();
            BindGridView();
        }
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string[] strSortExpression = ViewState["SortExpression"].ToString().Split(' ');
            if (strSortExpression[0] == e.SortExpression)
            {
                if (strSortExpression[1] == "ASC")
                {
                    ViewState["SortExpression"] = e.SortExpression + " " + "DESC";
                }
                else
                {
                    ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
                }
            }
            else
                ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
            BindGridView();
        }

        private void BindGridView()
        {
            DataTable dtPerson = LoadData();
            DataView dvPerson = new DataView(dtPerson);
            dvPerson.Sort = ViewState["SortExpression"].ToString();
            GridView1.DataSource = dvPerson;
            GridView1.DataBind();
        }

        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            BindGridView();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            BindGridView();

        }

    }
}